package com.douqu.game.core.config;

import com.douqu.game.core.config.common.CommonData;
import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.e.E_StableDataType;
import com.douqu.game.core.entity.GameObject;
import com.douqu.game.core.entity.ext.BagInfo;
import com.douqu.game.core.factory.ConfigFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.LoadFactory;
import com.douqu.game.core.factory.WordFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.CoreUtils;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2017-11-21 18:30
 */
public class StableDataConfig extends GameObject {

    public GoodsData[] goods;

    public int intValue;

    public int[] stringValue;

    public CommonData[] commons;

    public int[] intArray;

    @Override
    public boolean check()
    {
        //有些是客户端用的,这里不用检测是否为空
        E_StableDataType stableDataType = E_StableDataType.forNumber(id);
        if(goods == null && stableDataType != null && !stableDataType.isIgnoreGoods())
        {
            LogUtils.error("StableData check goods is null -> id:" + id + " name:" + name);
            return false;
        }
        if(stringValue == null)
        {
            LogUtils.error("StableData check stringValue is null -> id:" + id + " name:" + name);
            return false;
        }

        if(id == E_StableDataType.DIAMOND_MUCH_STABLE.getCode())
        {
            for(GoodsData goodsData : goods)
            {
                if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.CARD_KEY, goodsData.id) == null)
                {
                    LogUtils.error("StableData check goods error1 -> id:" + id + " name:" + name);
                    return false;
                }
                if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.CARD_KEY, goodsData.value) == null)
                {
                    LogUtils.error("StableData check goods error2 -> id:" + id + " name:" + name);
                    return false;
                }
            }
        }
        else if(id == E_StableDataType.CAMP_ACTIVITY_APPLY.getCode())
        {
            if(goods.length == 0)
            {
                LogUtils.error("StableData check goods error -> id:" + id + " name:" + name);
                return false;
            }
        }
        else if(id == E_StableDataType.WORLD_MAP.getCode())
        {
//            if(stringValue.length != 4)
//            {
//                LogUtils.error("StableData check stringValue error -> id:" + id + " name:" + name);
//                return false;
//            }

            ConfigFactory.WORLD_MAP_STATUS_WAIT_TIME  = stringValue[0] * 1000;//配置的是秒，转成毫秒
            ConfigFactory.WORLD_MAP_BATTLE_WAIT_TIME  = stringValue[1] * 1000;//配置的是秒，转成毫秒
            ConfigFactory.WORLD_MAP_BREAK_PERSON_MULT = stringValue[2];
            ConfigFactory.WORLD_MAP_BREAK_HP_RATE     = stringValue[3];

            LogUtils.info("ConfigFactory.WORLD_MAP_STATUS_WAIT_TIME  -> " + ConfigFactory.WORLD_MAP_STATUS_WAIT_TIME);
            LogUtils.info("ConfigFactory.WORLD_MAP_BATTLE_WAIT_TIME  -> " + ConfigFactory.WORLD_MAP_BATTLE_WAIT_TIME);
            LogUtils.info("ConfigFactory.WORLD_MAP_BREAK_PERSON_MULT -> " + ConfigFactory.WORLD_MAP_BREAK_PERSON_MULT);
            LogUtils.info("ConfigFactory.WORLD_MAP_BREAK_HP_RATE     -> " + ConfigFactory.WORLD_MAP_BREAK_HP_RATE);
            LogUtils.info("进大地图的等级                             -> " + intValue);

            int[] ary = new int[stringValue.length-4];
            for(int i = 0; i < ary.length; i++)
            {
                ary[i] = stringValue[i+4];
            }
            ConfigFactory.WORLD_MAP_FLUSH_TIME = ary;
        }
        return true;
    }


    public GoodsData getGoodsDataByQualiyu(int id){
        for (GoodsData good:goods){
            if(good.id == id){
                return good;
            }
        }
        return null;
    }

    public int getCampActivityRewardExploit()
    {
        if(goods == null)
            return 0;

        if(goods.length == 0)
            return 0;

        return goods[0].value;
    }

    /**
     *
     * @param rank
     * @return
     */
    public int getCampActivityRankRewardPlus(int rank)
    {
        if(rank < 0 || rank >= stringValue.length)
        {
            LogUtils.error("获取阵营战排名奖励加成错误 -> rank:" + rank);
            return 0;
        }

        return stringValue[rank];
    }


    public GoodsData getGoodsData(int i)
    {
        if(goods.length == 0)
            return null;

        int index = i < 0 ? 0 : i;
        index = index >= goods.length ? goods.length-1 : index;

        return goods[index];
    }

    /**
     * 钻石十连抽必中,暂时这样写,后面要优化
     * 前两次中固定的
     * @param camp
     * @param lotteryNum
     * @return
     */
    public int getDiamondMuchLotteryCardId(int camp, int lotteryNum)
    {
        //这里的goodsDate 解释一下
        //type 阵营  id第一个奖励的id  value第二个奖励的id
        for(GoodsData goodsData : goods)
        {
            if(camp == goodsData.type)
            {
                if(lotteryNum == 0)
                    return goodsData.id;
                if(lotteryNum == 1)
                    return goodsData.value;
                break;
            }
        }
        return 0;
    }
    

    public String checkGoods(BagInfo bagInfo, int i)
    {
        if(goods.length == 0)
            return null;

        int index = i < 0 ? 0 : i;
        index = index >= goods.length ? goods.length-1 : index;

        GoodsData goodsData = goods[index];
        if(bagInfo.getGoodsCount(goodsData.type, goodsData.id) < goodsData.value)
        {
            if(SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_ASSETS_VALUE == goodsData.type)
                return WordFactory.getWord(WordFactory.ASSET_NOT_ENOUGH, goodsData.id);
            else if(SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_PROPS_VALUE == goodsData.type)
                return WordFactory.getWord(WordFactory.GOODS_NOT_ENOUGH, goodsData.id);
            else
                return WordFactory.getWord(WordFactory.PARAM_ERROR);
        }
        else
        {
            bagInfo.subGoods(goodsData);
        }
        return null;
    }

    public int getFistCardId(int camp)
    {
        for(CommonData commonData : commons)
        {
            if(commonData.id == camp)
                return commonData.value;
        }
        return 0;
    }

    /**
     * 获取阵营战公共奖励
     * @return
     */
    public List<Integer> getCampActivityAuctionReward()
    {
        List<Integer> result = new ArrayList<>();
        for(CommonData commonData : commons)
        {
            if(com.bean.core.util.Utils.checkRandom10000(commonData.value))
                result.add(commonData.id);
        }
        LogUtils.debug("阵营战结束，加入拍卖行 -> " + result);
        return result;
    }

    /**
     * 获取衰减率
     * @param count
     * @return
     */
    public int getRepeatMul(int count){
        //总人数大于等于数组的length
        if (count >= stringValue.length){
            return stringValue[stringValue.length - 1];
        }else{
            return stringValue[count - 1];
        }
    }

    @Override
    public boolean setVariable(String key, String value)
    {
        if("goods".equals(key))
        {
            if (id == E_StableDataType.CUR_CRYSTAL.getCode()){
                // 0 表示curCryStal  1 表示maxCryStal
                intArray = LoadUtils.loadIntArray(key,value);
                return true;
            }
            else if (id == E_StableDataType.FIRST_LOTTERY_REWARD.getCode()){
                commons = LoadUtils.loadCommonDataToArray(key,value);
                for(CommonData commonData : commons)
                {
                    if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.CARD_KEY, commonData.value) == null)
                    {
                        LogUtils.error("Load InitData firstLotteryReward Error -> cardId:" + commonData.value);
                        return false;
                    }
                }
                return true;
            }
            else if (id == E_StableDataType.GLOBAL_NOTICE_CONDITION.getCode()){
                if(CoreUtils.isErrorValue(value))
                {
                    LogUtils.error("Load InitData GLOBAL_NOTICE_CONDITION Error -> cardId:" + value);
                    return false;
                }
                else
                {
                    intArray = LoadUtils.loadIntArray(key, value);
                }
                return true;
            }
            else if (id == E_StableDataType.CAMP_ACTIVITY_AUCTION_REWARD.getCode()
                    || id == E_StableDataType.CAMP_ACTIVITY_AUCTION_REWARD_SHOW.getCode()
                    || id == E_StableDataType.HEGEMONY_CITY_VALUE.getCode()
                    || id == E_StableDataType.HEGEMONY_KILLANDKILL_VALUE.getCode()
                    || id == E_StableDataType.CRYSTAL_GROW.getCode()
                    || id == E_StableDataType.EAT_GRASS_ADD_EXP.getCode()
                    || id == E_StableDataType.EAT_DRUG_ADD_EXP.getCode()){
                commons = LoadUtils.loadCommonDataToArray(key, value);
                return true;
            }

            goods = LoadUtils.loadGoodDataToArray(key, value);
            if(id == E_StableDataType.COLLECT_PRODUCT.getCode() ||
                    id == E_StableDataType.MAJOR_EXP.getCode() ||
                    id == E_StableDataType.COLLECT_SENIOR.getCode() ||
                    id == E_StableDataType.DIAMOND_MUCH_STABLE.getCode()||
                    id == E_StableDataType.COLLECT_PRODUCT_EXP.getCode() ||
                    id == E_StableDataType.WORLD_MAP.getCode() ||
                    id == E_StableDataType.CARD_START.getCode() ||
                    id == E_StableDataType.HEGEMONY_REPEAT_PLAYER_MUL.getCode() ||
                    id == E_StableDataType.HEGEMONY_START_END_TIME.getCode() ||
                    id == E_StableDataType.ARENA_RULE_FREE_TIMES.getCode()
                    )
            {
                return true;
            }
            return LoadUtils.checkGoods(goods, this.getClass(), key, id);
        }
        else if("stringValue".equals(key))
        {
            stringValue = LoadUtils.loadIntArray(key, value);
            return true;
        }
        else
            return super.setVariable(key, value);
    }



    @Override
    public String toString() {
        return "StableData{" +
                "goods=" + goods +
                ", intValue=" + intValue +
                "} " + super.toString();
    }
}
